package com.bt.lms.ui.action.teaching.arrange;

import com.bt.lms.ui.page.common.action.pageteaching.TeachingArrangeLabAction;
import com.bt.lms.ui.page.teaching.arrange.PageEditTeachingArrange;
import com.bt.lms.ui.page.teaching.arrange.PageTeachingArrangeList;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.control.Alert;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;

import java.time.LocalDate;

/**
 * 编辑教学安排保存按钮事件
 */
public class PageEditTeachingArrangeSaveBtnAction implements EventHandler {

    /**
     * 编辑教学安排界面
     */

    private PageEditTeachingArrange pageEditTeachingArrange;

    @Override

    public void handle(Event event) {

        String wroMes = "";
        //设置课程部分
        TextField setCourseTF = (TextField) pageEditTeachingArrange.lookup("#setCourseTF");
        String setCourseTFText = setCourseTF.getText();//获取设置课程文本框内容
        if (setCourseTFText.equals("")){//如果课程为空
            wroMes+="课程不能为空\r\n";

        }
        //设置教师部分
        TextField setTeacherTF = (TextField) pageEditTeachingArrange.lookup("#setTeacherTF");
        String setTeacherTFText = setTeacherTF.getText();//获取教师文本框内容
        if (setTeacherTFText.equals("")){//如果为空
            wroMes+="教师不能为空，至少选择一个教师\r\n";
        }
        //设置班级部分
        TextArea detailTA = (TextArea) pageEditTeachingArrange.lookup("#detailTA");
        String detailTAText = detailTA.getText();//获取到文本框内容
        if (detailTAText.equals("")){
            wroMes+="班级不能为空\r\n";
        }




        //开课时间部分
        DatePicker startCourseDP = (DatePicker) pageEditTeachingArrange.lookup("#startCourseDP");
        LocalDate startCourseDPValue = startCourseDP.getValue();
        if (startCourseDPValue==null){
            wroMes+="请设置开课时间，开课时间不能为空\r\n";
        }else {
            LocalDate now = LocalDate.now();//获取现在的时间
            int monthValue = LocalDate.now().getMonthValue();//获取月
            int dayOfMonth = LocalDate.now().getDayOfMonth();//获取月中的哪天

            int nowYear = now.getYear();//获取当前的年
            int year = startCourseDPValue.getYear();
            if (year<nowYear){
                wroMes+="请选择正确的时间\r\n";
            }
        }

        //结课时间部分
        DatePicker finishCourseDP = (DatePicker) pageEditTeachingArrange.lookup("#finishCourseDP");
        LocalDate finishCourseDPValue = finishCourseDP.getValue();//获取结课时间日期
        if (finishCourseDPValue==null){ //如果结课时间为空
            wroMes+= "结课时间不能为空\r\n";
        }else {
            LocalDate now = LocalDate.now();
            int nowYear = now.getYear();
            int year = finishCourseDPValue.getYear();
            int i = finishCourseDPValue.compareTo(startCourseDPValue);
            if (i<=0){//如果结课时间在开课时间之前
                wroMes+="结课时间不能在开课时间之前\r\n";

            }
        }



        //课程目标部分
        TextArea courseAimTA = (TextArea) pageEditTeachingArrange.lookup("#courseAimTA");
        String courseAimTAText = courseAimTA.getText();
        if (courseAimTAText.equals("")){
            wroMes+="课程目标不能为空\r\n";

        }
        if (courseAimTAText.length()>255){
            wroMes+="课程目标最大字数不能超过255\r\n";

        }


        //最后修改人部分
        TextField nameTF = (TextField) pageEditTeachingArrange.lookup("#nameTF");
        String nameTFText = nameTF.getText();//获取内容
        String nameTFTextTrim = nameTFText.trim();
        if (nameTFTextTrim.length()>32){
            wroMes+="最后修改人姓名不能超过32个字\r\n";
        }


        if (!"".equals(wroMes)){ //错误信息
            Alert alert = new Alert(Alert.AlertType.WARNING);
            alert.setContentText(wroMes);
            alert.showAndWait();
        }else{
            //正确信息
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setContentText("保存成功");
            alert.show();


            PageTeachingArrangeList pageTeachingArrangeList = new PageTeachingArrangeList();
            Pane pane = TeachingArrangeLabAction.pageSkipInfo.getPane();
            Integer index = TeachingArrangeLabAction.pageSkipInfo.getIndex();
            pane.getChildren().set(index, pageTeachingArrangeList);

        }






    }

    public PageEditTeachingArrange getPageEditTeachingArrange() {
        return pageEditTeachingArrange;
    }

    public void setPageEditTeachingArrange(PageEditTeachingArrange pageEditTeachingArrange) {
        this.pageEditTeachingArrange = pageEditTeachingArrange;
    }
}
